package com.xhwl.centiotdata.handler.device.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.bosiny.BosinyDeviceDTO;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.bosiny.DeviceBosinyHandler;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Falcon.cao @Copyright (C), 2022, 兴海物联
 * Date: 2022/6/15 11:42
 * Description: 包信息
 */
@Slf4j
@Service
public class DeviceIntegrationBosinyHandler extends DeviceIntegrationHandler {

    @Autowired
    private DeviceBosinyHandler deviceBosinyHandler;

    @Autowired
    private IDeviceCentService deviceCentService;

    private long mLastUpdateOnlineStatus = 0;

    @Override
    public void afterPropertiesSet() throws Exception {
        DeviceIntegrationFactory.register(ProductEnum.BOSINY.getId(), this);
    }

    @Override
    public void pullDevice(DeviceIntegrationConfigDTO configDTO) {
        switch (configDTO.getDeviceType()) {
            case 1:
                this.pullDoor(configDTO);
                break;
        }
    }

    private void pullDoor(DeviceIntegrationConfigDTO configDTO) {
        List<BosinyDeviceDTO> doors = deviceBosinyHandler.pullDoor(configDTO);  // 拉取数据
        if (!CollectionUtils.isEmpty(doors)) {
            List<DeviceDTO> devices = handleDevice(configDTO, doors);           // 转换数据
            ResultJson json = deviceCentService.asyncUpdateDevice(devices);     // 存储业务中台
            log.info("推送设备中台结果：{}", JSON.toJSONString(json));
        }
    }

    private List<DeviceDTO> handleDevice(DeviceIntegrationConfigDTO configDTO, List<BosinyDeviceDTO> doors) {
        return doors.stream().map(dto -> {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setProjectId(configDTO.getProjectId());
            deviceDTO.setProductId(configDTO.getProductId());
            deviceDTO.setCategoryId(configDTO.getCategoryEnum().getId());
            deviceDTO.setDeviceTypeId(configDTO.getDeviceType());
            deviceDTO.setName("门锁" + dto.getSn());
            deviceDTO.setThirdDeviceId(dto.getSn());
            deviceDTO.setAttributeJson(new JSONObject());
            return deviceDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public void pullDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        if (System.currentTimeMillis() - mLastUpdateOnlineStatus < 60 * 60 * 1000) {
            return;
        }
        List<BosinyDeviceDTO> doors = deviceBosinyHandler.pullDoorOnlineStatus(configDTO);
        if (!CollectionUtils.isEmpty(doors)) {
            List<Device> devices = handleDeviceOnlineStatus(configDTO, doors);      // 转换数据
            ResultJson json = deviceCentService.asyncUpdateOnlineStatus(devices);   // 存储业务中台
            log.info("推送设备中台结果：{}", JSON.toJSONString(json));
        }
    }

    private List<Device> handleDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO, List<BosinyDeviceDTO> doorDtos) {
        return doorDtos.stream().map(dto -> {
            Device device = new Device();
            device.setProjectId(configDTO.getProjectId());
            device.setProductId(configDTO.getProductId());
            device.setDeviceTypeId(configDTO.getDeviceType());
            device.setThirdDeviceId(dto.getSn());
            device.setOnlineStatus(Boolean.TRUE.equals(dto.getOnline()) ? DeviceOnlineStatusEnum.ONLINE : DeviceOnlineStatusEnum.OFFLINE);
            return device;
        }).collect(Collectors.toList());
    }
}
